package com.example.controller.sys;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.Dept;
import com.example.service.IDeptService;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 * 部门表 前端控制器
 * </p>
 * @author wsl
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/sys/dept")
@Api(tags = "部门管理",value = "")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @ApiOperation(value = "获取所有部门选项信息，也可用于查询指定公司的部门选项")
    @GetMapping("/options")
    public ResultVo getDeptOptions(Integer companyId){
        return deptService.getDeptOptions(companyId);
    }

    @ApiOperation(value = "获取所有部门信息")
    @GetMapping("/query")
    public ResultVo getDeptList(Integer companyId){
        return deptService.getDeptList(companyId);
    }

    @ApiOperation(value = "添加部门信息")
    @PostMapping("/add")
    public ResultVo addDepartment(@RequestBody Dept dept){
        return deptService.addDepartment(dept);
    }

    @ApiOperation(value = "修改部门信息")
    @PutMapping("/update")
    public ResultVo updateDepartment(@RequestBody Dept dept){
        if(deptService.updateById(dept)){
            return ResultVo.success("修改成功。");
        }
        return ResultVo.error("修改失败!");
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("/delete")
    public ResultVo deleteDepartment(Integer[] ids){
        if(deptService.removeByIds(Arrays.asList(ids))){
            return ResultVo.success("删除成功。");
        }
        return ResultVo.error("删除失败!");
    }

    @ApiOperation(value = "判断该公司下是否存在同名部门")
    @GetMapping("/has-some-deptName")
    public Boolean hasSomeDeptName(Integer id,
                                   @RequestParam(name = "deptName") String deptName,
                                   @RequestParam(name = "companyId") Integer companyId){
        return deptService.hasSomeDeptName(id,deptName,companyId);
    }

}
